package enjoyhiking.activity;

import enjoyhiking.R;
import enjoyhiking.service.EnjoyHikingService;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class DisplayActivity extends Activity
{
	public static final String 	DEBUG_TAG = "DisplayActivity";
	
	Button 				mCloseButton 			= null;
	EnjoyHikingService	mEnjoyHikingService		= null;
	
	Intent				mIntent					= null;
	
	/**
	 * established service connection
	 */
	ServiceConnection svcConn = new ServiceConnection() 
	{
		@Override
		public void onServiceDisconnected(ComponentName name) 
		{
			Log.i(DEBUG_TAG, "onServiceDisconnected()");
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) 
		{
			Log.i(DEBUG_TAG, "onServiceConnected()");

			mEnjoyHikingService = ((EnjoyHikingService.EnjoyHikingServiceBinder) service).getService();
		}
	};
	

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.display_activity_layout);
		
		mIntent = new Intent(this, EnjoyHikingService.class);
		
		initViews();
		initListenerForViews();
		
		new Handler().post(new Runnable() 
		{
			@Override
			public void run() 
			{
				if (bindService(mIntent, svcConn, 0) == false) 
				{
					Log.d(DEBUG_TAG, "FAIL TO BIND");
				} 
				else 
				{
					Log.d(DEBUG_TAG, "GOOD TO BIND");
				}
			}
		});
	}
	
	@Override
	protected void onDestroy() 
	{
		unbindService(svcConn);
		
		super.onDestroy();
	}
	
	
	private void initViews()
	{
		mCloseButton = (Button) findViewById(R.id.close_button);
	}
	
	private void initListenerForViews()
	{
		mCloseButton.setOnClickListener(new View.OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				mEnjoyHikingService.close();
				finish();
			}
		});
	}
	
}
